package framework.common.zbar;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import framework.common.Constant;
import framework.common.alertview.AlertView;
import framework.common.alertview.OnItemClickListener;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.model.NewsTabModelResult;
import hk.m4s.lr.repair.test.model.NewsTableModel;
import hk.m4s.lr.repair.test.model.TwoCodeModel;
import hk.m4s.lr.repair.test.service.AccountSerive;
import hk.m4s.lr.repair.test.service.news.NewsService;
import hk.m4s.lr.repair.test.ui.anount.QcanErroActivity;
import hk.m4s.lr.repair.test.ui.baoyang.LookBaoYangActivity;
import hk.m4s.lr.repair.test.ui.baoyang.SqcanDeviceBaoActivity;
import hk.m4s.lr.repair.test.ui.equipment.QcanRepaireActivity;
import hk.m4s.lr.repair.test.ui.equipment.SqcanDeviceWeixiuActivity;
import hk.m4s.lr.repair.test.utils.CustomDialog;
import okhttp3.Response;


/**
 * Created by qi.wang on 2017/2/17.
 */

public class CaptureActivity extends io.github.xudaojie.qrcodelib.CaptureActivity implements OnItemClickListener{
    private Context context;
    private String showIntent = "1";
    private String  result;
    private String type="1";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=this;
        showIntent=getIntent().getStringExtra("type");
    }

    @Override
    protected void handleResult(String resultString) {
        if (resultString != null) {
            try {
                //if (strs.length > 0) {
                // String[] im = strs[1].split("=");
                if (showIntent != null) {
                    if (showIntent.equals("1")) {
                        Intent resultIntent = new Intent(CaptureActivity.this, CustomDialog.class);
                        Bundle bundles = new Bundle();
                        resultIntent.putExtra("result",resultString);
                        startActivity(resultIntent);
                        finish();
                    }else if (showIntent.equals("2")) {
                        result=resultString;
                        type="1";
                        new AlertView("提示", "确定报修吗?", "取消", new String[]{"确定"}, null, this, AlertView.Style.Alert, this).setCancelable(true).show();
                    }else if(showIntent.equals("3")){
                        result=resultString;
                        type="2";
                        new AlertView("提示", "确定借用吗?", "取消", new String[]{"确定"}, null, this, AlertView.Style.Alert, this).setCancelable(true).show();
                    }else if(showIntent.equals("6")){
                        String intentLook=getIntent().getStringExtra("intents");
                        Intent in;
                        if(intentLook.equals("1")){
                            in = new Intent(CaptureActivity.this, SqcanDeviceWeixiuActivity.class);
                            in.putExtra("deciceState", "2");
                        }else{
                            in = new Intent(CaptureActivity.this, SqcanDeviceBaoActivity.class);
                            in.putExtra("deciceState", "1");
                        }
                        in.putExtra("decice_id", resultString);
                        startActivity(in);
                        finish();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                Intent intent=new Intent(CaptureActivity.this,QcanErroActivity.class);
                startActivity(intent);
                finish();
            }
        } else {
            Intent intent=new Intent(CaptureActivity.this,QcanErroActivity.class);
            startActivity(intent);
            finish();
            restartPreview();
        }
    }
    public void deviceByTwoCode(String id) {
        try {
            Map<String, Object> parmas = new HashMap<>();
            parmas.put("device_type", Constant.device_type);
            parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
            JSONObject jsondata = new JSONObject();
            jsondata.put("id", id);
            parmas.put("jsonText", jsondata.toString());
            AccountSerive.deviceByTwoCode(context, parmas, new ResponseCallback<TwoCodeModel>() {
                @Override
                public void onSuccess(TwoCodeModel accountModel) {
                    Intent resultIntent = new Intent(CaptureActivity.this, QcanRepaireActivity.class);
                    resultIntent.putExtra("accountModel",accountModel);
                    startActivity(resultIntent);
                    finish();
                }

                @Override
                public void onError(Response response, int errorCode, String errorMsg, Exception e) {
                    restartPreview();
                }
            });
        }catch (JSONException e){
            e.printStackTrace();
        }

    }
    public void toolUse(String id) {
        try {
            Map<String, Object> parmas = new HashMap<>();
            parmas.put("device_type", Constant.device_type);
            parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
            JSONObject jsondata = new JSONObject();
            jsondata.put("id", id);
            parmas.put("jsonText", jsondata.toString());
            AccountSerive.toolUse(context, parmas, new ResponseCallback<TwoCodeModel>() {
                @Override
                public void onSuccess(TwoCodeModel accountModel) {
                    finish();
                }

                @Override
                public void onError(Response response, int errorCode, String errorMsg, Exception e) {
                    restartPreview();
                }
            });
        }catch (JSONException e){
            e.printStackTrace();
        }

    }
    @Override
    public void onItemClick(Object o, int position) {
        //判断是否是拓展窗口View，而且点击的是非取消按钮
        if (position == -1) {
            restartPreview();
            return;
        } else {
            if (position == 0) {
                if(type.equals("1")){
                    deviceByTwoCode(result);
                }else {
                    toolUse(result);
                }
            }
        }
    }
}
